<?php
class ModelCatalogCarBranch extends Model {
    public function addCarBranch($data) {
        foreach ($data['car_branch_description'] as $language_id => $value) {
            $car_branch_name= $this->db->escape($value['name']);
            break;
        }
        
       $query  = "INSERT INTO " . DB_PREFIX . "car_branch SET parent_id = '".(int)$data['parent_id']."', name='".$car_branch_name."',";
       $query .= " sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status']."',";
       $query .= " date_modified = NOW(), date_added = NOW()";
       $this->db->query( $query );
    
        $car_branch_id = $this->db->getLastId();
        
        if (isset($data['image'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "car_branch SET image = '" . $this->db->escape($data['image']) . "' WHERE car_branch_id = '" . (int)$car_branch_id . "'");
        }

        if ($data['keyword']) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'car_branch_id=" . (int)$car_branch_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
        }
        
        $this->cache->delete('car_branch');
    }
    
    public function editCarBranch($car_branch_id, $data) {
        foreach ($data['car_branch_description'] as $language_id => $value) {
            $car_branch_name= $this->db->escape($value['name']);
            break;
        }
        $query  = "UPDATE " . DB_PREFIX . "car_branch SET parent_id = '" . (int)$data['parent_id'] . "', name='".$car_branch_name."',";
        $query .= " sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status']."',";
        $query .= " date_modified = NOW() WHERE car_branch_id = '" . (int)$car_branch_id . "'";
        $this->db->query($query);

        if (isset($data['image'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "car_branch SET image = '" . $this->db->escape($data['image']) . "' WHERE car_branch_id = '" . (int)$car_branch_id . "'");
        }
        $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'car_branch_id=" . (int)$car_branch_id. "'");
        
        if ($data['keyword']) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'car_branch_id=" . (int)$car_branch_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
        }
        $this->cache->delete('car_branch');
    }
    
    public function deleteCarBranch($car_branch_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "car_branch WHERE car_branch_id = '" . (int)$car_branch_id . "'");
        
        
        
        $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'car_branch_id=" . (int)$car_branch_id . "'");
        
        $query = $this->db->query("SELECT car_branch_id FROM " . DB_PREFIX . "car_branch WHERE parent_id = '" . (int)$car_branch_id . "'");

        foreach ($query->rows as $result) {
            $this->deleteCarBranch($result['car_branch_id']);
        }
        
        $this->cache->delete('car_branch');
    } 

    public function getCarBranch($car_branch_id) {
        $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'car_branch_id=" . (int)$car_branch_id . "') AS keyword FROM " . DB_PREFIX . "car_branch WHERE car_branch_id = '" . (int)$car_branch_id . "'");
        
        return $query->row;
    } 
    
    public function getCarBranches($parent_id) {
        $car_branch_data = $this->cache->get('car_branch.' . $this->config->get('config_language_id') . '.' . $parent_id);
    
        if (!$car_branch_data) {
            $car_branch_data = array();
        
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "car_branch c WHERE c.parent_id = '" . (int)$parent_id . "' ORDER BY c.sort_order, c.name ASC");
        
            foreach ($query->rows as $result) {
                $car_branch_data[] = array(
                    'car_branch_id' => $result['car_branch_id'],
                    'name'        => $this->getPath($result['car_branch_id'], $this->config->get('config_language_id')),
                    'status'        => $result['status'],
                    'sort_order'  => $result['sort_order']
                );
            
                $car_branch_data = array_merge($car_branch_data, $this->getCarBranches($result['car_branch_id']));
            }    
    
            $this->cache->set('car_branch.' . $this->config->get('config_language_id') . '.' . $parent_id, $car_branch_data);
        }
        return $car_branch_data;
    }
    
    public function getPath($car_branch_id) {
        $query = $this->db->query("SELECT name, parent_id FROM " . DB_PREFIX . "car_branch AS c WHERE c.car_branch_id = '" . (int)$car_branch_id . "' ORDER BY c.sort_order, c.name ASC");
        
        $car_branch_info = $query->row;
        
        if ($car_branch_info['parent_id']) {
            return $this->getPath($car_branch_info['parent_id'], $this->config->get('config_language_id')) . $this->language->get('text_separator') . $car_branch_info['name'];
        } else {
            return $car_branch_info['name'];
        }
    }
    
    public function getCarBranchImgFolder($car_branch_ids) {
        
        $query = $this->db->query("SELECT image FROM " . DB_PREFIX . "car_branch WHERE car_branch_id IN (".$car_branch_ids.") LIMIT 1 ");
        return $query->row;        
        
    }
        



        
    public function getTotalCategories() {
          $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "car_branch");
        
        return $query->row['total'];
    }    
        
    public function getTotalCategoriesByImageId($image_id) {
          $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "car_branch WHERE image_id = '" . (int)$image_id . "'");
        
        return $query->row['total'];
    }

	public function getCarBranchesName($category_id) {
		$category_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "car_branch_description LIMIT 1");
		foreach ($query->rows as $result) {
			$category_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}
		
		return $category_description_data;
	}    


}
?>